<?php
/**
 * Created by PhpStorm.
 * User: 王峥宇
 * Date: 2019-04-30
 * Time: 0:29
 */

namespace app\admin\model;

use app\admin\model\User as UserModel;
use app\admin\model\BaseModel;
use app\admin\model\Indent;
use think\Csv;
use think\Request;
use think\Session;

class Rengong extends BaseModel
{
    public static function getRen(){
        $gid = Session::get('admin_uid');

        $id = new Guanli();
        $id = $id->where('id',$gid)->find();

        $ren = self::where('uid',$id['uid'])->find();
        if($ren==''){
            if (request()->isAjax()) {
                $data['weixin'] = input('weixin');
                $data['qq'] = input('qq');
                $data['uid']=$id['uid'];
                $up = self::insert($data);
                if ($up){
                    return 1;
                }else{
                    return 2;
                }
            }
        }else {
            if (request()->isAjax()) {
                $data['weixin'] = input('weixin');
                $data['qq'] = input('qq');
                $up = self::where('id',$ren['id'])->update($data);
                if($up){
                    return 1;
                }else{
                    return 2;
                }
            }
        }
        return $ren;
    }
    public static function xiugai()
    {
        $file = \request()->file('excel');
        $info = $file->move(ROOT_PATH . 'upload' . DS . 'exel');
        $filename = ROOT_PATH . 'upload' . DS . 'exel/' . $info->getSaveName();
        $handle = fopen($filename, 'r');
        $arr2= array(0=>'time',1=>'zhai',2=>'jiao',3=>'china',4=>'money',5=>'jsmoney',6=>'jzmoney',7=>'bizhong',8=>'jizhang'
        ,9=>'zhichu',10=>'jibi',11=>'yue',12=>'name',13=>' ');
        while ($data = fgetcsv($handle)) {
            $arr[] = $data;
        }
        $arr3 = array();
        foreach ($arr as $k => $val)
        {
            if ($k < 7) {
                continue;
            }
            //如果最后一位没有空 改成13
            if(count($val)<14){
                unset($val);
            }else{
                foreach($val as $kk=>$vv){
                    $id[$arr2[$kk]] = $vv;
                }
                $arr3[] = $id;
            }
        }
        foreach ($arr3 as $v)
        {
                if(trim($v['jizhang'])!='')
                {
                     $time = date('Y-m-d H:i:s');
                     $arr6 = UserModel::where('u_nickname',trim($v['name']))->find();
                     //$arr1[] = str_replace(',','',trim($v['jizhang']));
                     //查询出符合条件的订单
                     $indent =  Indent::where('uid',$arr6['uid'])->where('t_money',str_replace(',','',trim($v['jizhang'])))->find();
                    //为用户改变订单状态
                    $din =  Indent::where('uid',$arr6['uid'])->where('t_money',str_replace(',','',trim($v['jizhang'])))->find();
                    if($din!='') {
//                        //为当前用户的保证金添加数值
//                        $umoney = User::where('uid',$arr6['uid'])->setInc('u_money',str_replace(',','',trim($v['jizhang'])));
                        //查询流水表
                        $log = Balancelog::where('uid',$arr6['uid'])->where('type',2)->field('num')->select();
                        if($log!=[]){
                            foreach ($log as $v)
                            {
                                $a[] = $v['num'];
                            }
                            //旧的总佣金
                            $b = array_sum($a);
                            //新的总佣金
                            $c = $din['t_money'] * $arr6['lilv'];
                        }else{
                            $b = 0;
                            $c = $din['t_money'] * $arr6['lilv'];
                        }
                        $d =bcadd($b,$c,2);
                        //添加流水
                        $inlog = Balancelog::insert([
                            'uid'=>$arr6['uid'], 'type'=>2,
                            'num'=>$c, 'from_type'=>2, 'before_num'=>$b,
                            'after_num'=>$d, 'memo'=>'完成订单佣金+'.$c,
                            'createtime'=>$time, 'zid'=>$din['zid']
                        ]);
                        //为jc_log添加记录
                        $jclog = Log::insert([
                            'l_number'=>$din['t_number'],
                            'uid'=>$arr6['uid'],
                            'zid'=>$din['zid'],
                            'l_yongjin'=>$c,
                            'w_createtime'=>$time
                        ]);
                    }
                }
        }dump($inlog);die;
        if(file_exists($filename))
        {
            fclose($handle); // 关闭指针，也就相当于关掉文件
            unset($info);
            unlink($filename);
        }
    }
}